/**
 * @Description:
 * @Author BPF
 * @create 2021/9/1 11:20
 */
import { requireAllModule, isObject } from '@bianpengfei/utils'

const apisModules = requireAllModule({
  requireContext: require.context('./modules/', false, /\.js$/),
  exclude: ['./modules/index.js']
})
  .reduce((pre, [cur, moduleName]) => {
    if (cur?.default) {
      const name = moduleName.replace(/^\.\/(.*)\.\w+$/, '$1')
      // let res = {}
      // if (isObject(cur.default)) {
      //   res = Object.entries(cur.default).reduce((p, [ck, cv]) => {
      //     pre[name + ck] = cv
      //     return pre
      //   }, {})
      // }
      pre.push({
        [name]: cur?.default
      })
    }
    return pre
  }, [])
  .flat(Infinity)
  .reduce((pre, cur) => {
    if (isObject(cur)) {
      pre = { ...pre, ...cur }
    }
    return pre
  }, {})

export default {
  ...apisModules,
  install: (app) => {
    app.prototype.$api = apisModules
  }
}
